package com.nuanshui.heatedloan.credit.pay.behavior;

import com.alibaba.fastjson.JSON;
import com.nuanshui.framework.behavior.BehaviorAttribute;
import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.framework.security.DESC;
import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.credit.facade.ApplyOrderService;
import com.nuanshui.heatedloan.credit.facade.CustomerBankCardInfoService;
import com.nuanshui.heatedloan.credit.facade.CustomerIdCardInfoService;
import com.nuanshui.heatedloan.credit.facade.PayOrderInfoService;
import com.nuanshui.heatedloan.credit.pay.OrderPayStateService;
import com.nuanshui.heatedloan.credit.pay.PayRequest;
import com.nuanshui.heatedloan.credit.pay.TransRequestCode;
import com.nuanshui.heatedloan.credit.pay.annotation.PayBehaviorAttribute;
import com.nuanshui.heatedloan.credit.pay.constant.PayBehaviorType;
import com.nuanshui.heatedloan.credit.pay.utils.AllinPayUtil;
import com.nuanshui.heatedloan.credit.pay.validation.ValidationResult;
import com.nuanshui.heatedloan.credit.vo.CustomerBankCardInfoVO;
import com.nuanshui.heatedloan.credit.vo.CustomerIdCardInfoVO;
import com.nuanshui.heatedloan.credit.vo.PayOrderInfoVO;
import org.nutz.log.impl.LogInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * 准备订单相关信息
 */
@PayBehaviorAttribute(value= PayBehaviorType.PrepareOrderBehavior,order=100)
public class PrepareOrderBehavior extends PayBehavior {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private PayOrderInfoService payOrderInfoService;

    @Autowired
    private CustomerIdCardInfoService customerIdCardInfoService;

    @Autowired
    private CustomerBankCardInfoService customerBankCardInfoService;

    @Autowired
    private ApplyOrderService applyOrderService;

    @Override
    public void internalInvoke(PayRequest request) {

        fillRequest(request);
    }

    protected void fillRequest(PayRequest request) {

        AllinPayUtil.logOrderPay(logger, request, "订单数据准备开始");

        if (request.getOrder() == null) {
            //查询订单
            PayOrderInfoVO payOrderInfoVO = payOrderInfoService.loadVOByOrderNo(request.getOrderNo());
            request.setOrder(payOrderInfoVO);
        } else {
            request.setOrderNo(request.getOrder().getOrderNo());
        }


        if (request.getOrder() != null) {
            //获取银行卡信息

            String mixCardNo = null;
            if (SysStringUtils.isNotEmpty(request.getMixCardNo())) {
                mixCardNo = request.getMixCardNo();
                AllinPayUtil.logOrderPay(logger, request, "订单数据准备 当前为非默认银行账号:" + DESC.decrypt3DES(mixCardNo));
            } else {
                mixCardNo = request.getOrder().getMixCardNo();
                AllinPayUtil.logOrderPay(logger, request, "订单数据准备 当前为默认银行账号:" + DESC.decrypt3DES(mixCardNo));
            }

            CustomerBankCardInfoVO customerBankCardInfoVO = applyOrderService.getRepayCardInfo(request.getOrder(),
                    mixCardNo,request.getTransRequestCode());

            request.setBankCard(customerBankCardInfoVO);

            //获取身份信息
            CustomerIdCardInfoVO customerIdCardInfoVO = customerIdCardInfoService.loadByUserId(request.getOrder().getUserId());

            request.setIdCard(customerIdCardInfoVO);

            AllinPayUtil.logOrderPay(logger, request, "订单数据准备结束 订单信息: " + JSON.toJSONString(request.getOrder()));
            AllinPayUtil.logOrderPay(logger, request, "订单数据准备结束 银行信息: " + JSON.toJSONString(request.getBankCard()));
            AllinPayUtil.logOrderPay(logger, request, "订单数据准备结束 身份信息: " + JSON.toJSONString(request.getIdCard()));
        }
    }

    @Override
    public void internalValidate(ValidationResult result, PayRequest request) {
        super.internalValidate(result, request);
    }
}
